package com.example.note;

//import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.example.note.controller.Controller;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.style.BulletSpan;

public class DatabaseHanler extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "NoteDB";
	private static final String TABLE_NOTE = "Notes";

	private static final String KEY_ID = "id";
	private static final String KEY_TITLE = "title";
	private static final String KEY_CONTENT = "content";
	private static final String KEY_IMAGE = "image";
	private static final String KEY_UPDATEAT = "update_at";
	Controller CONTROLLER;

	public DatabaseHanler(Context context) {
		// TODO Auto-generated constructor stub
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String CREATE_NOTE_TABLE = "CREATE TABLE " + TABLE_NOTE + " (" + KEY_ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + KEY_TITLE
				+ " TEXT, " + KEY_CONTENT + " TEXT, " + KEY_IMAGE + " TEXT, "
				+ KEY_UPDATEAT + " TEXT)";
		db.execSQL(CREATE_NOTE_TABLE);
	}

	// update database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP DATABASE IF EXISTS " + DATABASE_NAME);
		onCreate(db);
	}

	// get datetime system

	public Notes getNote(int id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(TABLE_NOTE, new String[] { KEY_TITLE,
				KEY_UPDATEAT }, "id=?", new String[] { String.valueOf(id) },
				null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		Notes n = new Notes(cursor.getString(0), cursor.getString(1));
		return n;

	}

	/**
	 * public ArrayList<Notes> getNotes() { ArrayList<Notes> notelist = new
	 * ArrayList<Notes>();
	 * 
	 * String selectQuery = "SELECT " + KEY_TITLE + "," + KEY_UPDATEAT +
	 * " FROM " + TABLE_NOTE; SQLiteDatabase db = this.getWritableDatabase();
	 * Cursor cursor = db.rawQuery(selectQuery, null);
	 * 
	 * if (cursor.moveToFirst()) { do { Notes notes = new
	 * Notes(cursor.getString(0), cursor.getString(1)); //
	 * notes.setTitle(cursor.getString(1)); // notes notelist.add(notes); }
	 * while (cursor.moveToNext()); } return notelist; }
	 */
	public ArrayList<Notes> getAllNote() {
		ArrayList<Notes> notelist = new ArrayList<Notes>();

		String selectQuery = "SELECT * FROM " + TABLE_NOTE;
		SQLiteDatabase db = this.getWritableDatabase();

		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				Notes notes = new Notes();
				notes.setID(Integer.parseInt(cursor.getString(0)));
				notes.setTitle(cursor.getString(1));
				notes.setContent(cursor.getString(2));
				notes.setImg(cursor.getString(3));
				notes.setUpdateAt(cursor.getString(4));

				notelist.add(notes);
			} while (cursor.moveToNext());
		}
		return notelist;
	}

	public int getNotesCout() {
		String sqlQuery = "SELECT * FROM " + TABLE_NOTE;
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.rawQuery(sqlQuery, null);
		cursor.close();

		return cursor.getCount();

	}

	// add notes

	public void addNote(Notes n) {
		SQLiteDatabase db = this.getReadableDatabase();
		ContentValues value = new ContentValues();
		value.put(KEY_TITLE, n.getTitle());
		value.put(KEY_CONTENT, n.getContent());
		value.put(KEY_IMAGE, n.getImg());
		value.put(KEY_UPDATEAT, getCurTime());
		db.insert(TABLE_NOTE, null, value);
		db.close();
	}

	// update record
	public void updateNote(Notes notes) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put(KEY_TITLE, notes.getTitle());
		value.put(KEY_CONTENT, notes.getContent());
		value.put(KEY_IMAGE, notes.getImg());
		value.put(KEY_UPDATEAT, getCurTime());
		db.update(TABLE_NOTE, value, KEY_ID + " = ?",
				new String[] { String.valueOf(notes.getID()) });
	}

	// delete record
	public void deleteNote(String id) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NOTE, KEY_ID + " = ?", new String[] { id });
		db.close();
	}
	public String getCurTime() {
		// Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+7"));
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat date = new SimpleDateFormat("HH:mm");
		Date currentLocalTime = cal.getTime();
		date.setTimeZone(TimeZone.getTimeZone("GMT+7"));
		String localTime = date.format(currentLocalTime);

		return localTime;
	}
	// short content

}
